<template>
  <vue-scroll class="page-usermanagement">
    <div class="page-header card-base header-accent">
      <h1>
        <span>用户管理</span>
        <div class="mt-10">
          <el-button type="primary" @click="dialogFormVisible = true"> <i class="mdi mdi-plus"></i> 新增 </el-button>
        </div>
      </h1>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">
          <i class="mdi mdi-home-outline"></i>
        </el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/usermanagement' }">用户</el-breadcrumb-item>
        <el-breadcrumb-item>列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <el-row>
      <el-col>
        <div class="table-box card-base card-shadow--medium">
          <el-form :model="query" label-width="60px" label-position="right" class="query">
            <el-col :span="22">
              <el-col :span="6" class="col-p">
                <el-form-item label="账号">
                  <el-input type="text" v-model="query.username"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6" class="col-p">
                <el-form-item label="邮箱">
                  <el-input type="email" v-model="query.email"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6" class="col-p">
                <el-form-item label="电话">
                  <el-input type="text" v-model="query.phonenumber"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6" class="col-p">
                <el-form-item>
                  <el-button class="mr-20" type="primary" @click="queryClick">查询</el-button>
                  <el-button @click="resetClick">重置</el-button>
                </el-form-item>
              </el-col>
            </el-col>
          </el-form>

          <el-table :data="tableData.slice((currentPage - 1) * pagesize, currentPage * pagesize)" style="width: 100%">
            <el-table-column prop="username" label="账号" sortable width="120"></el-table-column>
            <el-table-column prop="fullname" label="全名" width="120"></el-table-column>
            <el-table-column prop="email" label="邮箱" width="180"></el-table-column>
            <el-table-column prop="phonenumber" label="电话" width="120"></el-table-column>
            <el-table-column prop="birthdate" label="生日" width="120">
              <template slot-scope="scope">{{ scope.row.birthdate | date }}</template>
            </el-table-column>
            <el-table-column prop="gender" label="性别" width="80">
              <template slot-scope="scope">{{ scope.row.gender == 1 ? "男" : "女" }}</template>
            </el-table-column>
            <el-table-column prop="hobbies" label="爱好" width="140"></el-table-column>
            <el-table-column prop="createdtime" label="建立日期" width="120">
              <template slot-scope="scope">{{ scope.row.createdtime | date }}</template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="150">
              <template slot-scope="scope">
                <el-button @click="editClick(scope.row)" type="text" size="small">修改</el-button>
                <el-button @click="deleteClick(scope.row)" type="text" size="small">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            class="paging"
            background
            :current-page="currentPage"
            layout="prev, pager, next"
            :total="total"
            @current-change="currentChange"
          ></el-pagination>
        </div>
      </el-col>
    </el-row>
    <el-dialog title="新增/修改用户" :visible.sync="dialogFormVisible" :close-on-click-modal="false" @close="closeDialog" width="35%">
      <el-form :model="form">
        <el-form-item label="账号" :label-width="formLabelWidth">
          <el-input name="username" v-model="form.username" v-validate="'required'" data-vv-as="账号" auto-complete="off"></el-input>
          <span class="error">{{ errorBags.first("username") }}</span>
        </el-form-item>
        <el-form-item label="密码(默认：12345678)" :label-width="formLabelWidth">
          <el-input name="password" type="password" v-model="form.password" v-validate="'required'" data-vv-as="密码" auto-complete="off"></el-input>
          <span class="error">{{ errorBags.first("password") }}</span>
        </el-form-item>
        <el-form-item label="全名" :label-width="formLabelWidth">
          <el-input name="fullname" v-model="form.fullname" v-validate="'required'" data-vv-as="全名" auto-complete="off"></el-input>
          <span class="error">{{ errorBags.first("fullname") }}</span>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input name="email" type="email" v-model="form.email" v-validate="'required'" data-vv-as="邮箱" auto-complete="off"></el-input>
          <span class="error">{{ errorBags.first("email") }}</span>
        </el-form-item>
        <el-form-item label="电话" type="phone" :label-width="formLabelWidth">
          <el-input name="phonenumber" v-model="form.phonenumber" v-validate="'required'" data-vv-as="电话" auto-complete="off"></el-input>
          <span class="error">{{ errorBags.first("phonenumber") }}</span>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submitClick">提交</el-button>
      </span>
    </el-dialog>
  </vue-scroll>
</template>

<script>
import axios from "axios";
export default {
  name: "usermanagement",
  data() {
    return {
      tableData: [],
      total: 0,
      pagesize: 10,
      currentPage: 1,
      dialogFormVisible: false,
      form: {
        username: "",
        password: "12345678",
        fullname: "",
        email: "",
        phonenumber: "",
      },
      query: {
        username: "",
        email: "",
        phonenumber: "",
      },
      formLabelWidth: "120px",
    };
  },
  methods: {
    queryClick() {
      axios.get("/api/user", { params: { ...this.query, pagesize: this.pagesize, currentPage: this.currentPage } }).then((response) => {
        this.tableData = response.data.data;
        this.total = response.data.data.length;
        this.currentPage = 1;
      });
    },
    resetClick() {
      this.query = { username: "", email: "", phonenumber: "" };
    },
    deleteClick(row) {
      this.$confirm("此操作会永久删除此账号，确定继续?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          axios
            .post("/api/user/Remove/" + row.id)
            .then((response) => {
              this.tableData = response.data.data;
              this.total = response.data.data.length;
              this.currentPage = 1;
              // console.log(response.data);
              this.$message({
                type: "success",
                message: "删除成功",
              });
            })
            .catch((ex) => {
              console.log(ex);
            });
        })
        .catch((err) => {
          console.log(err);
        });
    },
    editClick(row) {
      axios.get("/api/user/" + row.username).then((response) => {
        this.form = response.data.data;
        this.dialogFormVisible = true;
      });
    },
    currentChange: function(currentPage) {
      this.currentPage = currentPage;
    },
    submitClick() {
      this.$validator.validate().then((valid) => {
        if (!valid) {
          // do stuff if not valid.
          return;
        } else {
          axios
            .post("/api/user", this.form)
            .then((response) => {
              this.dialogFormVisible = false;
              this.tableData = response.data.data;
              this.total = response.data.data.length;
              this.currentPage = 1;
              this.$message({
                type: "success",
                message: response.data.message,
              });
            })
            .catch((ex) => {
              console.log(ex);
            });
        }
      });
    },
    closeDialog() {
      this.form.name = "";
      this.form.address = "";
      this.form.id = 0;
      this.errorBags.clear();
    },
  },
  created() {
    axios.get("/api/user", { params: { ...this.query, pagesize: 10, currentPage: 1 } }).then((response) => {
      this.tableData = response.data.data;
      this.total = response.data.data.length;
      this.currentPage = 1;
    });
  },
  watch: {
    // dialogFormVisible(newValue) {
    //   if (!newValue) {
    //     this.form = { name: "", address: "" };
    //   }
    // }
  },
};
</script>

<style lang="scss" scoped>
@import "../../assets/scss/_variables";
</style>

<style lang="scss">
.el-table .cell {
  padding-left: 25px;
}
.page-usermanagement {
  .query {
    margin: 30px auto;

    .el-form-item {
      margin-bottom: 16px;
    }

    .el-form-item__content {
      line-height: 14px;
    }

    .col-p {
      padding: 0 10px;
      box-sizing: border-box;
    }
  }
  .sep-line {
    height: 1px;
    margin: 0 20px;
  }
  .page-header {
    margin-bottom: 8px;
  }
  .el-form-item {
    margin-bottom: 16px;
  }
  .el-table th,
  .el-table td {
    padding: 5px 0;
  }
  .el-form-item__content {
    line-height: 14px;
  }
  .error {
    color: #d7195d;
    font-size: 12px;
    line-height: 1;
    padding-top: 4px;
  }
  h1 {
    button {
      float: right;
      padding: 10px 25px;
      background: rgba(255, 255, 255, 0.2);
      text-transform: uppercase;
      border: none;
      outline: none;
      cursor: pointer;
      color: white;
      opacity: 0.8;
      font-family: inherit;

      i {
        position: relative;
        top: 1px;
      }

      &:hover {
        opacity: 1;
      }
    }
  }
  .table-box {
    overflow: auto;
  }
  .paging {
    float: right;
    padding-right: 40px;
    margin-top: 10px;
    margin-bottom: 10px;
  }
}

@media (max-width: 768px) {
  .page-usermanagement {
    .vb-content {
      padding: 0 5px !important;
      margin: -5px;
      width: calc(100% + 10px) !important;
    }
  }
}
</style>
